<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>File Upload Example</title>
</head>
<body>
    <h1>File Upload Example</h1>
    <input type="file" id="fileInput">
    <button id="uploadButton">Upload</button>

    <script>
        const uploadButton = document.getElementById('uploadButton');
        const fileInput = document.getElementById('fileInput');

        uploadButton.addEventListener('click', async () => {
            const selectedFile = fileInput.files[0];
            
            if (!selectedFile) {
                alert('Please select a file.');
                return;
            }

            const formData = new FormData();
            formData.append('media', selectedFile);
            const token =  '72_VZRihhXTfFomsAfOW2E_9xyo84w3vDFSoZBluLVQvCSh3Sg52Aibgy-LpkRb2EzEH_1vV8JXoKK6pkVQDp_hsZX63Mj64FYjOcA-6kc6JhC7arE-BIIH0rxT9coZVQbAHAFBW'
            const url = 'https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token' + token
            try {
                const response = await fetch(url, {
                    method: 'POST',
                    body: formData
                });

                if (response.ok) {
                    alert(response);
                } else {
                    alert('Failed to upload file.');
                }
            } catch (error) {
                console.error('Error uploading file:', error.message);
                alert('An error occurred while uploading the file.');
            }
        });
    </script>
</body>
</html>
